<template>
  <div class="container">
    <div class="app-container" style="padding: 10px;">
      <el-button type="primary" size="mini" plain @click="showDialog = true">添加角色</el-button>
      <el-table :data="list">
        <el-table-column label="角色" width="200" align="center" prop="name">
          <template #default="{row}">
            <el-input v-if="row.isEdit" v-model="row.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>

        </el-table-column>
        <el-table-column label="启用" width="200" align="center" prop="state">
          <template #default="{row}">
            <el-switch v-if="row.isEdit" v-model="row.state" :active-value="1" :inactive-value="0" />
            <span v-else>{{ row.state === 1 ? '已启用' : row.state === 0 ? '未启用' : '其他状态' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="描述" align="center" prop="description">
          <template #default="{row}">
            <el-input v-if="row.isEdit" v-model="row.description" size="mini" type="textarea" :rows="2" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <div v-if="row.isEdit">
              <el-button type="primary" size="mini" plain @click="confirm(row)">确认</el-button>
              <el-button type="danger" size="mini" plain @click="cancel(row)">取消</el-button>
            </div>
            <div v-else>
              <el-button type="text" size="mini">分配权限</el-button>
              <el-button type="text" size="mini" @click="edit(row)">编辑</el-button>
              <el-popconfirm
                title="您确定删除吗？"
                @onConfirm="deleteId(row.id)"
              >
                <el-button slot="reference" type="text" size="mini">删除</el-button>
              </el-popconfirm>
            </div>

          </template>
        </el-table-column>
      </el-table>
      <el-row style="height: 40;" type="flex" justify="end" align="center">
        <el-pagination
          layout="prev, pager, next"
          :total="pageParams.total"
          :current-page="pageParams.page"
          :page-size="pageParams.pagesize"
          @current-change="handle"
        />
      </el-row>
    </div>
    <!-- 弹层组件 -->
    <el-dialog title="新增角色" :visible.sync="showDialog" width="500px" @close="cancelClick">
      <!-- 角色表单 -->
      <el-form ref="form" label-width="100px" :model="role" :rules="roleRules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="role.name" size="mini" style="width: 300px" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch v-model="role.state" :active-value="1" :inactive-value="0" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="role.description" :rows="3" type="textarea" size="mini" style="width: 300px" />
        </el-form-item>
        <el-form-item>
          <el-row style="width: 300px" type="flex" justify="center">
            <el-button size="mini" type="primary" @click="confirmClick">确定</el-button>
            <el-button size="mini" @click="cancelClick">取消</el-button>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, addRole, edit, deleteApi } from '@/api/role'
import { set } from 'vue'
export default {
  name: 'Role',
  data() {
    return {
      list: [],
      pageParams: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      showDialog: false,
      roleRules: {
        name: [{ required: true, message: '来将可留姓名', trigger: 'blur' }],
        description: [{ required: true, message: '来将可留姓名', trigger: 'blur' }]
      },
      role: {
        name: '',
        state: 1,
        description: ''
      }

    }
  },
  created() {
    this.getRole()
  },
  methods: {
    async getRole() {
      const { rows, total } = await getRoleList(this.pageParams)
      this.list = rows
      this.pageParams.total = total
      this.list.forEach(item => {
        // 响应式属性添加
        set(item, 'isEdit', false)
        set(item, 'edit', {
          name: item.name,
          state: item.state,
          description: item.description
        })
        // this.$set(item, 'isEdit', false)
      })
    },
    handle(currentPage) {
      this.pageParams.page = currentPage
      this.getRole()
    },
    confirmClick() {
      this.$refs.form.validate(async(isOk) => {
        if (isOk) {
          console.log(this.role)
          await addRole(this.role)
          this.$message.success('添加角色成功')
          this.getRole()
          this.cancelClick()
        }
      })
    },
    cancelClick() {
      this.$refs.form.resetFields()
      this.showDialog = false
    },
    edit(row) {
      row.isEdit = true
    },
    cancel(row) {
      row.isEdit = false
      row.name = row.edit.name
      row.state = row.edit.state
      row.description = row.edit.description
    },
    async confirm(row) {
      if (row.name && row.description) {
        await edit(row)
        this.$message.success('数据修改成功')
        // eslint-disable-next-line require-atomic-updates
        row.isEdit = false
      } else {
        this.$message.danger('请保证数据完整性')
      }
    },
    async deleteId(id) {
      await deleteApi(id)
      this.$message.success('删除数据成功')
      if (this.pageParams.page > 1 && this.list.length === 1) {
        this.pageParams.page--
      }
      this.getRole()
    }

  }
}
</script>
